/*
 * @(#)AssEvolve.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.ass;

import fr.ema.arch3d.ref.Fractal;
import fr.ema.arch3d.ref.GenericFactoryConnector.GenericFactoryConnector;
import fr.ema.arch3d.model.architecture.configuration.ComponentClass;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.factory.GenericFactory;
import org.objectweb.fractal.api.type.TypeFactory;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class AssEvolve {
    public static void cmpReplace(String oldCmpName, String itf, String newCmpName, String motivation, int testTimes,
                                  String condition) {
        Component   oldCmp = null,
                    newCmp = null;
        Component[] a;

        try {
            org.objectweb.fractal.api.Component boot;

            boot = Fractal.getBootstrapComponent();

            TypeFactory             tf  = Fractal.getTypeFactory(boot);
            GenericFactory          gf  = Fractal.getGenericFactory(boot);
            GenericFactoryConnector gfc = new GenericFactoryConnector();

            a = Fractal.getContentController(DrawAssImpl.container).getFcSubComponents();

            for (int i = 0; i < a.length; i++) {
                if (Fractal.getNameController(a[i]).getFcName().equals(oldCmpName)) {
                    oldCmp = Fractal.getContentController(DrawAssImpl.container).getFcSubComponents()[i];
                }
            }

            ComponentClass newc = (ComponentClass) Fractal.getComponentDescriptorController(oldCmp).getComponent();

            newc.setImplementation("helloworld.ServerImpl2");
            newCmp         = gfc.newFcInstanceComponent(newCmpName, newc);;
            Fractal.getNameController(newCmp).setFcName(newCmpName);
            Fractal.getElementtypeController(newCmp).setElementType("component");
            Fractal.getComponentDescriptorController(newCmp).setComponent(
                Fractal.getComponentDescriptorController(oldCmp).getComponent());
            Fractal.getEvolutionController(DrawAssImpl.container).ComponentEvolution(oldCmp, "p", newCmp, "corrective",
                                           null);
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DrawAssImpl.refreshContainerGraph(newCmpName, "replace");
    }

    public static void printResult() {
        System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
    }
}
